/* SWIG Python interface for alvar library 
 * Faraz Mirzaei, MARS Lab, University of Minnesota 
 * faraz@cs.umn.edu */

%module alvar

%{
//#include "cv_select.h"
#include "highgui.h"
#include "cv.h"
#include "cxcore.h"
#define MAX_FILENAME_LEN 128
extern int DetectMarker(CvPoint2D64f**, char*, int*, int);
extern void release_images();
%}

%inline %{

static int MAX_MARKERS;
static CvPoint2D64f** CvPntArr;
static int* id;
static int num_markers;
static char filename[MAX_FILENAME_LEN];

void init(int max_markers) {
    MAX_MARKERS = max_markers;
    CvPntArr = new CvPoint2D64f*[MAX_MARKERS];
    for (int k = 0 ; k < MAX_MARKERS ; k++)
        CvPntArr[k] = new CvPoint2D64f[4];
    id = new int[MAX_MARKERS];
}

int get_markers(char* fname) {
	strncpy(filename, fname, MAX_FILENAME_LEN);
	num_markers = DetectMarker(CvPntArr, fname, id, MAX_MARKERS);
	return num_markers;
}

CvPoint2D64f* get_corners(int ind_marker, int ind_corner) {
    return &CvPntArr[ind_marker][ind_corner];
}

int get_id(int ind_marker) {
    return id[ind_marker];
}

void release(void) {
    for (int k = 0 ; k < MAX_MARKERS ; k++)
        delete CvPntArr[k];
    delete CvPntArr;
    delete id;
    release_images();
}

void show_markers(void) {
	const char * windowName = "window";
	cvNamedWindow(windowName);
	IplImage* img = cvLoadImage(filename);
	for (int i = 0; i < num_markers; i++) {
		for (int j = 0; j < 4; j++) {
			// get corners to display
			CvPoint2D64f* corner1 = get_corners(i, j);
			CvPoint2D64f* corner2 = get_corners(i, (j+1)%4);
			// and convert them to integers to make them displayable
			CvPoint c1 = cvPoint((int)corner1->x, (int)corner1->y);
			CvPoint c2 = cvPoint((int)corner2->x, (int)corner2->y);
			cvLine(img, c1, c2, CV_RGB(0, 100, 0), 4);
			}
		}
	cvShowImage(windowName, img);
	cvWaitKey();
}

%}


%include "cv_select.h"
extern int DetectMarker(CvPoint2D64f**, char*, int*, int);

